﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Dev101.Web.Models
{
    public class Dev101Model
    {
        private System.Web.Routing.RequestContext requestContext;
        public Dev101Model(System.Web.Routing.RequestContext requestContext)
        {
            this.requestContext = requestContext;
        }
        public void GetTagContainer()
        {            
            Dictionary<string, int> pageItems = new Dictionary<string,int>();
            JsonHandle<Dev101.Models.TagContainer> jsonHandle = new JsonHandle<Dev101.Models.TagContainer>(ConfigurationManager.AppSettings["DataDump"] + "tags.txt");
            jsonHandle.Deserialize();
            tagContainer = jsonHandle.Element;
            foreach(var tag in tagContainer.tagsForCurrentPage)
            {
                tag.tagpagecount = (tag.numberOfCourses / 20) + 1;
            }
        }
        public void GetTrainingItemsByTagId(string tagId)
        {

        }
        private Dev101.Models.TagContainer tagContainer;

        public Dev101.Models.TagContainer TagContainer
        {
            get { return tagContainer; }
            set { tagContainer = value; }
        }        
    }
    public class JsonHandle<T>
    {
        string jsonString = string.Empty;
        string fileName = string.Empty;
        T element;
        public JsonHandle(string fileName)
        {
            this.fileName = fileName;
        }
        public void Deserialize()
        {
            jsonString = File.ReadAllText(fileName);
            element = JsonConvert.DeserializeObject<T>(jsonString);
        }
        public T Element { get { return element; } }
    }
}